 

import Control.*;
import Model.*;

/**
 * UI for Suppliers
 * @author Stefan
 */
public class SupplierUI {

    private AssociateController assCtr = AssociateController.getInstance();

    /**
     * Start Supplier menu
     */
    public void start()
    {
        suppliersMenu();
    }

    /**
     * Supplier menu
     */
    private void suppliersMenu()
    {
        boolean exit = false;
        while (!exit)
        {
            int choise = writeSuppliersMenu();
            if (choise == 1)
            {
                startCreate(CommonUIFunctions.readString("\n write ID: "), CommonUIFunctions.readString("\n write name: "), CommonUIFunctions.readString("\n write address: "), CommonUIFunctions.readString("\n write company: "));
            }
            else if (choise == 2)
            {
                startRead(CommonUIFunctions.readString("\n write Id of the Supplier you want to find "));
            }
            else if (choise == 3)
            {
                startUpdate(CommonUIFunctions.readString("\n write the id of Supplier update: "));
            }
            else if (choise == 4)
            {
                startDelete(CommonUIFunctions.readString("\n write Id of the Supplier you want to delete: "));
            }
            else
            {
                exit = true;
            }
        }
    }

    /**
     * Print Supplier menu
     * @return int based on your choise
     */
    private int writeSuppliersMenu()
    {
        System.out.println("\f *** Suppliers menu ***");
        System.out.println(" (1) Create");
        System.out.println(" (2) Read");
        System.out.println(" (3) Update");
        System.out.println(" (4) Delete");
        System.out.println(" (0) Back");

        return CommonUIFunctions.readInt("\n Make your choise: ");
    }

    /**
     * Create menu
     * @param id
     * @param name
     * @param address
     * @param company
     */
    private void startCreate(String id, String name, String address, String company)
    {
        if (assCtr.findSupplierID(id) == null)
        {
            assCtr.addSupplier(id, name, address, company);
        }
        else
        {
           startCreate(CommonUIFunctions.readString("Supplier ID is already in use, choose another: "), name, address, company);
        }
    }

    /**
     * Read menu
     * @param id
     */
    private void startRead(String id)
    {
        try
        {
            getInfo(id);
        }
        catch (Exception e)
        {
            System.out.println("There was no user with this ID");
        }
    }

    /**
     * Delete menu
     * @param id
     */
    private void startDelete(String id)
    {
        if (assCtr.deleteAssociateID(id) == false)
        {
            System.out.println("There was no user with this ID");
        }
    }

    /**
     * Start Update menu
     * @param id
     */
    private void startUpdate(String id)
    {
        try
        {
            Supplier sup = getInfo(id);
            updateMenu(sup);
        }
        catch (Exception e)
        {
            System.out.println("There was no user with this ID");
        }
    }

    /**
     * Update menu
     * @param sup
     */
    private void updateMenu(Supplier sup)
    {
        boolean exit = false;
        while (!exit)
        {
            int choise = writeUpdateMenu();
            if (choise == 1)
            {
                String newID = CommonUIFunctions.readString("\n write new ID: ");
                //If ID is in use try again
                while (assCtr.checkExistingSupplierID(newID))
                {
                    newID = CommonUIFunctions.readString("\n Supplier ID is already in use, choose another: ");
                }
                sup.setId(newID);
            }
            else if (choise == 2)
            {
                sup.setName(CommonUIFunctions.readString("\n write new Name: "));
            }
            else if (choise == 3)
            {
                sup.setAddress(CommonUIFunctions.readString("\n write new Address: "));
            }
            else if (choise == 4)
            {
                sup.setCompany(CommonUIFunctions.readString("\n write new Company: "));
            }
            else
            {
                exit = true;
            }
        }
    }

    /**
     * Print update menu
     * @return
     */
    private int writeUpdateMenu()
    {
        System.out.println("\f *** UpdateMenu ***");
        System.out.println("\f Choose one to edit");
        System.out.println(" (1) Id ");
        System.out.println(" (2) Name");
        System.out.println(" (3) Address");
        System.out.println(" (4) Company");
        System.out.println(" (0) Back");

        return CommonUIFunctions.readInt("\n Make your choise: ");
    }

    /**
     * Get info about Supplier
     * @param id
     * @return Supplier
     */
    private Supplier getInfo(String id)
    {
        Supplier sup = assCtr.findSupplierID(id);
        System.out.println("Supplier \n" + "Id: " + sup.getId() + "\nName: " + sup.getName() + "\nCompany: " + sup.getCompany() + "\nAddress: " + sup.getAddress());
        return sup;
    }
}
